package com.feizhi.inventory;

import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;

import androidx.appcompat.app.AppCompatActivity;

import com.feizhi.inventory.databinding.ActivityMainBinding;
import com.feizhi.inventory.fragment.Inventorydetails.InventoryDetailsFragment;
import com.jeremyliao.liveeventbus.LiveEventBus;

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding mBinding;
    private int key;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(mBinding.getRoot());

        initView();

    }

    private void initView() {



    }
    boolean flag=false;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        key = keyCode;




        if (keyCode == /*142*/619 || keyCode == 619&&!flag) {

//            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                // 按键按下时的操作
                Log.d("KeyEvent", "Key pressed: " + keyCode);
                Log.d("按键监听-----", "onKeyDown: " + keyCode);
                flag=true;
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                LiveEventBus.get().with("keyCode").post(keyCode);
//            } else if (event.getAction() == KeyEvent.ACTION_UP) {
//
//            }


        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode == 142 || keyCode == 619) {
            flag=false;
            InventoryDetailsFragment.start=false;
            // 按键抬起时的操作
            Log.d("KeyEvent", "Key released: " + keyCode);
            Log.d("KeyEvent", "Key pressed: " + keyCode);
            Log.d("按键监听-----", "onKeyDown: " + keyCode);
            flag=true;
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            LiveEventBus.get().with("keyCode1").post(keyCode);
       }
        return super.onKeyUp(keyCode, event);
    }
}